package com.wz.dhs.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wz.dhs.exception.BusinessException;
import com.wz.dhs.mapper.user.SysUserMapper;
import com.wz.dhs.mapper.user.SysUserRoleMapper;
import com.wz.dhs.model.PageHandler;
import com.wz.dhs.model.common.PageInfo;
import com.wz.dhs.po.user.SysUserEntity;
import com.wz.dhs.po.user.SysUserRole;
import com.wz.dhs.service.base.ISysUserService;
import com.wz.dhs.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 管理账号信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-20 21:51:41
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询管理账号信息
     *
     * @param id 管理账号信息主键
     * @return 管理账号信息
     */
    @Override
    public SysUserEntity selectSysUserById(Long id) {
        return sysUserMapper.selectById(id);
    }

    /**
     * 查询管理账号信息列表
     *
     * @param sysUser 管理账号信息
     * @return 管理账号信息
     */
    @Override
    public PageInfo<SysUserEntity> selectSysUserList(SysUserEntity sysUser) {
        Page<SysUserEntity> page = sysUserMapper.selectPage(new Page<SysUserEntity>(sysUser.getPageNum(), sysUser.getPageSize()),
                new LambdaQueryWrapper<SysUserEntity>()
                        .eq(Objects.nonNull(sysUser.getAccountStatus()), SysUserEntity::getAccountStatus, sysUser.getAccountStatus())
                        .like(StringUtils.isNotBlank(sysUser.getUsername()), SysUserEntity::getUsername, sysUser.getUsername())
                        .like(StringUtils.isNotBlank(sysUser.getPhone()), SysUserEntity::getPhone, sysUser.getPhone()));


        return PageHandler.returnPage(page.getRecords(), sysUser, page.getTotal());
    }

    /**
     * 新增管理账号信息
     *
     * @param sysUser 管理账号信息
     * @return 结果
     */
    @Override
    public void insertSysUser(SysUserEntity sysUser) {
        Long count = this.sysUserMapper.selectCount(new LambdaQueryWrapper<SysUserEntity>()
                .eq(SysUserEntity::getUsername, sysUser.getUsername())
                .or()
                .eq(SysUserEntity::getPhone, sysUser.getPhone()));
        if(count > 0){
            throw new BusinessException("10059");
        }
        sysUser.setPasswordEncry(passwordEncoder.encode(sysUser.getPassword()));
        sysUserMapper.insert(sysUser);
    }

    /**
     * 修改管理账号信息
     *
     * @param sysUser 管理账号信息
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUserEntity sysUser) {
        sysUser.setPasswordEncry(passwordEncoder.encode(sysUser.getPassword()));
        return sysUserMapper.updateById(sysUser);
    }

    /**
     * 批量删除管理账号信息
     *
     * @param ids 需要删除的管理账号信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByIds(Long[] ids) {
        List<Long> userIds = Arrays.asList(ids);
        sysUserMapper.deleteBatchIds(userIds);
        return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
                .in(SysUserRole::getUserId, userIds));
    }

    /**
     * 删除管理账号信息信息
     *
     * @param id 管理账号信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserById(Long id) {
        userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, id));
        return sysUserMapper.deleteById(id);
    }
}

